<!-- subpkg_archive/list/index.vue -->
<script lang="ts" setup>
  import { patientListApi, removePatientApi } from '@/apis/patient'
  import type { PatientItem } from '@/types/patient'
  import { onShow } from '@dcloudio/uni-app'
  import { onMounted, ref } from 'vue'
  import { useConsultStore } from '@/stores/consult'
  const store = useConsultStore()

  interface IParams {
    type?: number
  }
  const props = defineProps<IParams>()

  const init = () => {
    // 判断当前显示患者档案列表 还是  选择患者
    if (props.type) {
      // 选择患者
      console.log('选择患者')
      uni.setNavigationBarTitle({ title: '选择患者' })
    } else {
      // 正常 患者档案
      uni.setNavigationBarTitle({ title: '患者档案' })
    }
  }
  init()

  // 1. 患者列表
  const patinetList = ref<PatientItem[]>([])
  // 家庭档案（患者）列表
  async function getPatientList() {
    // 患者列表接口
    const data = await patientListApi()
    // 渲染接口数据
    patinetList.value = data
    console.log(data)
  }
  onShow(() => {
    getPatientList()
  })

  const swipeOptions = ref([
    {
      text: '删除',
      style: {
        backgroundColor: '#dd524d',
        color: '',
        fontSize: '',
      },
    },
    {
      text: '编辑',
      style: {
        backgroundColor: '#ff9900',
        color: '',
        fontSize: '',
      },
    },
  ])

  const onDelete = async (id: string) => {
    // 2. 弹出确认框，询问用户是否删除
    // 3. 获取待删除的元素的id ，发送给后端 执行删除
    // 4. 先提示删除成功
    // 5. 自己删除本地数组中的对应数据  - 重新发送一次请求获取最新数据
    const res = await uni.showModal({
      title: '警告',
      content: '您是否确定删除',
    })
    // console.log(res)
    if (res.confirm) {
      await removePatientApi(id)
      uni.showToast({ title: '删除成功', icon: 'none' })
      // 删除本地数据
      const index = patinetList.value.findIndex((v) => v.id === id)
      patinetList.value.splice(index, 1)
    }
  }
  const goNextPage = () => {
    uni.navigateTo({ url: '/subpkg_consult/payment/index' })
  }
</script>

<template>
  <scroll-view>
    <view class="archive-page">
      <view class="archive-tips">最多可添加6人 {{ store.patientId }} </view>
      <uni-swipe-action>
        <uni-swipe-action-item
          v-for="(patient, index) in patinetList"
          :key="patient.id"
          :right-options="swipeOptions"
          @click="onDelete(patient.id)"
        >
          <!-- 在选择患者页面 触发可以选择 -->
          <view
            @click="props.type && store.setPatientId(patient.id)"
            :class="{ active: patient.id === store.patientId }"
            class="archive-card"
          >
            <view class="archive-info">
              <text class="name">{{ patient.name }}</text>
              <text class="id-card">
                {{ patient.idCard.replace(/^(.{6}).+(.{4})$/, '$1********$2') }}
              </text>
              <text v-if="patient.defaultFlag === 1" class="default">默认</text>
            </view>
            <view class="archive-info">
              <text class="gender">{{ patient.genderValue }}</text>
              <text class="age">{{ patient.age }}岁</text>
            </view>
            <navigator
              hover-class="none"
              class="edit-link"
              :url="`/subpkg_archive/form/index?id=${patient.id}`"
            >
              <uni-icons type="icon-edit" size="20" color="#16C2A3" custom-prefix="iconfont" />
            </navigator>
          </view>
        </uni-swipe-action-item>
      </uni-swipe-action>

      <!-- 添加按钮 -->
      <view v-if="patinetList.length < 6" class="archive-card">
        <navigator class="add-link" hover-class="none" url="/subpkg_archive/form/index">
          <uni-icons color="#16C2A3" size="24" type="plusempty" />
          <text class="label">添加患者</text>
        </navigator>
      </view>
    </view>
  </scroll-view>
  <button v-if="type && patinetList.length" @click="goNextPage">下一步</button>
</template>

<style lang="scss">
  @import './index.scss';
</style>
